/**
 * Copyright (c) 华南农业大学信息学院蔡超敏2015版权所有
 * 
 * 文件创建时间：2015年5月3日
 */
package org.scauinfo.fpos.action.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.scauinfo.fpos.action.BaseAction;
import org.scauinfo.fpos.model.SaleorderitemModel;
import org.scauinfo.fpos.service.ProductService;
import org.scauinfo.fpos.service.SaleorderService;
import org.scauinfo.fpos.service.SaleorderitemService;
import org.scauinfo.fpos.util.ExceptionUtil;
import org.scauinfo.fpos.util.Json;
import org.scauinfo.fpos.vo.Product;
import org.scauinfo.fpos.vo.Saleorder;
import org.scauinfo.fpos.vo.Saleorderitem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;

/**
 * @author 蔡超敏
 * 
 */
@Namespace("/")
@Scope("prototype")
@Controller
@Action(value = "saleorderitem")
public class SaleorderitemAction extends BaseAction implements
		ModelDriven<Saleorderitem> {

	private static final long serialVersionUID = 3088843221530537631L;
	private static final Logger logger = Logger
			.getLogger(SaleorderitemAction.class);
	@Autowired
	private SaleorderitemService saleorderitemService;

	@Autowired
	private SaleorderService saleorderService;

	@Autowired
	private ProductService productService;

	public Product product = new Product();
	public Saleorder saleorder = new Saleorder();
	public Saleorderitem saleorderitem = new Saleorderitem(saleorder, product);

	/**
	 * 得到数据列表
	 */
	public void grid() {
		System.out.println("tags ----1");
		int total = saleorderitemService.getTotal();
		System.out.println("tags ----total" + total + "page" + page + "rows"
				+ rows + ", sort" + sort + ", order" + order);
		List<Saleorderitem> list = saleorderitemService.findAllSaleorderitem(
				page, rows, sort, order);
		System.out.println("tags ----2" + list.size());

		List<SaleorderitemModel> mList = new ArrayList<SaleorderitemModel>();

		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Saleorderitem saleorderitem = (Saleorderitem) iterator.next();
			SaleorderitemModel SaleorderitemModel = new SaleorderitemModel(
					saleorderitem.getId(), saleorderitem.getProduct()
							.getProductId(), saleorderitem.getProduct()
							.getProductName(), saleorderitem.getProduct()
							.getProductPrice(), saleorderitem.getProduct()
							.getImage(), saleorderitem.getProduct()
							.getProductType().getProductTypeId(), saleorderitem
							.getProduct().getProductType().getTypeName(),
					saleorderitem.getProduct().getProductType()
							.getDescription(), saleorderitem.getCount(),
					saleorderitem.getDescription());
			mList.add(SaleorderitemModel);
		}
		writeJson(total, mList);
		System.out.println("tags ----3");
	}

	/**
	 * 添加操作
	 */
	public void add() {
		Json result = new Json();

		System.out.println("tags ----Saleorderitem" + saleorderitem.getId());
		try {
			saleorderitem.setProduct(productService.get(saleorderitem.getProduct().getId()));
			saleorderitem.setSaleorder(saleorderService.get(saleorderitem.getSaleorder().getId()));
			
			saleorderitemService.saveSaleorderitem(saleorderitem);
			result.setSuccess(true);
			result.setMsg("添加商品项成功");
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("添加商品项失败");
		}
		writeJson(result);
	}

	/**
	 * 修改操作
	 */
	public void edit() {
		Json result = new Json();
		try {
			saleorderitem.setProduct(productService.get(saleorderitem.getProduct().getId()));
			saleorderitem.setSaleorder(saleorderService.get(saleorderitem.getSaleorder().getId()));
			
			saleorderitemService.updateSaleorderitem(saleorderitem);
			result.setSuccess(true);
			result.setMsg("修改商品项成功");
			result.setSuccess(true);
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("修改商品项失败");
		}
		writeJson(result);
	}

	/**
	 * 删除操作
	 */
	public void remove() {
		Json result = new Json();
		try {
			System.out.println("tags ----=" + saleorderitem.getId());
			saleorderitemService.deleteSaleorderitem(saleorderitem.getId());
			result.setSuccess(true);
			result.setMsg("删除商品项成功");
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("删除商品项失败");
		}
		writeJson(result);
	}

	@Override
	public Saleorderitem getModel() {
		return saleorderitem;
	}
}